#!/bin/bash
#--------------------------------------------------
# This script is used for: 
#   to download the binaries needed for installing a cluster addon with ica-ansible
# @author:   lmy
# @usage:    ./icadown
# @repo:     https://github.com/
#--------------------------------------------------
set -o nounset
set -o errexit
set -o pipefail
#set -o xtrace

cat ./group_vars/all.yml | grep -e "version" -e "software_dir" -e "image" | sed "s/: /=/g" > ./tmp
. ./tmp

function usage() {
  echo -e "\033[33mUsage:\033[0m icadown [options] [args]"
  cat <<EOF
  option:
    -D              download default binaries into "$software_dir"
    -P <package>    download packages of the binaries (mysql,docker,zookeeper...)
EOF
}

function usage-down-packages(){
  echo -e "\033[33mUsage:\033[0m ezdown -P <package>"
  cat <<EOF
available PKes:
    mysql             to download package of MySQL
    docker            to download package of Docker
    zookeeper         to download package of Zookeeper
    nginx             to download package of nginx
    keepalived        to download package of keepalived
    maven             to download package of maven
    jdk               to download package of jdk
    kafka             to download package of kafka
    prometheus        to download package of prometheus
    node_exporter     to download package of node_exporter
    alertmanager      to download package of alertmanager
    elasticsearch     to download package of elasticsearch
    kibana            to download package of kibana
    filebeat          to download package of filebeat
    logstash          to download package of logstash
    harbor            to download package of harbor
    gitlab-runner-image     to download package of gitlab-runner image
examples:
    ./ezdown -P mysql
EOF
}

function logger() {
  TIMESTAMP=$(date +'%Y-%m-%d %H:%M:%S')
  case "$1" in
    debug)
      echo -e "$TIMESTAMP \033[36mDEBUG\033[0m $2"
      ;;
    info)
      echo -e "$TIMESTAMP \033[32mINFO\033[0m $2"
      ;;
    warn)
      echo -e "$TIMESTAMP \033[33mWARN\033[0m $2"
      ;;
    error)
      echo -e "$TIMESTAMP \033[31mERROR\033[0m $2"
      ;;
    *)
      ;;
  esac
}

function download_mysql() {
  MYSQL_URL="https://cdn.mysql.com/archives/mysql-8.0/mysql-${mysql_version}-linux-glibc2.12-${ARCH}.tar.xz"

  if [[ -f "$BASE/mysql-${mysql_version}-linux-glibc2.12-${ARCH}.tar.xz" ]];then
    logger warn "mysql binaries already existed"
  else
    logger info "downloading mysql binaries, arch:$ARCH, version:${mysql_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$MYSQL_URL" || { logger error "downloading mysql failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$MYSQL_URL" || { logger error "downloading mysql failed"; exit 1; }
    fi
    mv -f "./mysql-${mysql_version}-linux-glibc2.12-${ARCH}.tar.xz" "$BASE/"
  fi
}

function download_docker() {
  DOCKER_URL="https://mirrors.aliyun.com/docker-ce/linux/static/stable/${ARCH}/docker-${docker_version}.tgz"
  DOCKER_COMPOSE_URL="https://github.com/docker/compose/releases/download/v${docker_compose_version}/docker-compose-linux-$ARCH"

  if [[ -f "$BASE/docker-${docker_version}.tgz" ]];then
    logger warn "docker binaries already existed"
  else
    logger info "downloading docker binaries, arch:$ARCH, version:${docker_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$DOCKER_URL" || { logger error "downloading docker failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$DOCKER_URL" || { logger error "downloading docker failed"; exit 1; }
    fi
    mv -f "./docker-${docker_version}.tgz" "$BASE/"
  fi

  if [[ -f "$BASE/docker-compose-linux-$ARCH" ]];then
    logger warn "docker-compose binaries already existed"
  else
    logger info "downloading docker-compose binaries, arch:$ARCH, version:${docker_compose_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$DOCKER_COMPOSE_URL" || { logger error "downloading docker-compose failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$DOCKER_COMPOSE_URL" || { logger error "downloading docker-compose failed"; exit 1; }
    fi
    mv -f "./docker-compose-linux-$ARCH" "$BASE/docker-compose"
  fi
}

function download_zookeeper() {
  ZOOKEEPER_URL="https://archive.apache.org/dist/zookeeper/zookeeper-${zookeeper_version}/apache-zookeeper-${zookeeper_version}-bin.tar.gz"

  if [[ -f "$BASE/apache-zookeeper-${zookeeper_version}-bin.tar.gz" ]];then
    logger warn "zookeeper binaries already existed"
  else
    logger info "downloading zookeeper binaries, arch:$ARCH, version:${zookeeper_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$ZOOKEEPER_URL" || { logger error "downloading zookeeper failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$ZOOKEEPER_URL" || { logger error "downloading zookeeper failed"; exit 1; }
    fi
    mv -f "./apache-zookeeper-${zookeeper_version}-bin.tar.gz" "$BASE/"
  fi
}

function download_nginx() {
  NGINX_URL="https://nginx.org/download/nginx-${nginx_version}.tar.gz"

  if [[ -f "$BASE/nginx-${nginx_version}.tar.gz" ]];then
    logger warn "nginx binaries already existed"
  else
    logger info "downloading nginx binaries, arch:$ARCH, version:${nginx_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$NGINX_URL" || { logger error "downloading nginx failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$NGINX_URL" || { logger error "downloading nginx failed"; exit 1; }
    fi
    mv -f "./nginx-${nginx_version}.tar.gz" "$BASE/"
  fi
}

function download_keepalived() {
  KEEPALIVED_URL="https://www.keepalived.org/software/keepalived-${keepalived_version}.tar.gz"

  if [[ -f "$BASE/keepalived-${keepalived_version}.tar.gz" ]];then
    logger warn "keepalived binaries already existed"
  else
    logger info "downloading keepalived binaries, arch:$ARCH, version:${keepalived_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$KEEPALIVED_URL" || { logger error "downloading keepalived failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$KEEPALIVED_URL" || { logger error "downloading keepalived failed"; exit 1; }
    fi
    mv -f "./keepalived-${keepalived_version}.tar.gz" "$BASE/"
  fi
}

function download_jdk() {
  JDK_URL="https://repo.huaweicloud.com/java/jdk/8u151-b12/${jdk_version}"

  if [[ -f "$BASE/${jdk_version}" ]];then
    logger warn "jdk binaries already existed"
  else
    logger info "downloading jdk binaries, arch:$ARCH, version:${jdk_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$JDK_URL" || { logger error "downloading jdk failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$JDK_URL" || { logger error "downloading jdk failed"; exit 1; }
    fi
    mv -f "./${jdk_version}" "$BASE/"
  fi
}

function download_maven() {
  MAVEN_URL="https://archive.apache.org/dist/maven/maven-3/${maven_version}/binaries/apache-maven-${maven_version}-bin.tar.gz"

  if [[ -f "$BASE/nginx-${maven_version}.tar.gz" ]];then
    logger warn "maven binaries already existed"
  else
    logger info "downloading maven binaries, arch:$ARCH, version:${maven_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$MAVEN_URL" || { logger error "downloading maven failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$MAVEN_URL" || { logger error "downloading maven failed"; exit 1; }
    fi
    mv -f "./apache-maven-${maven_version}-bin.tar.gz" "$BASE/"
  fi
}

function download_kafka() {
  KAFKA_URL="https://archive.apache.org/dist/kafka/${kafka_version}/kafka_2.13-${kafka_version}.tgz"

  if [[ -f "$BASE/kafka_2.13-${kafka_version}.tgz" ]];then
    logger warn "kafka binaries already existed"
  else
    logger info "downloading kafka binaries, arch:$ARCH, version:${kafka_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$KAFKA_URL" || { logger error "downloading kafka failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$KAFKA_URL" || { logger error "downloading kafka failed"; exit 1; }
    fi
    mv -f "./kafka_2.13-${kafka_version}.tgz" "$BASE/"
  fi
}

function download_prometheus() {
  PROMETHEUS_URL="https://github.com/prometheus/prometheus/releases/download/v${prometheus_version}/prometheus-${prometheus_version}.linux-amd64.tar.gz"

  if [[ -f "$BASE/prometheus-${prometheus_version}.linux-amd64.tar.gz" ]];then
    logger warn "prometheus binaries already existed"
  else
    logger info "downloading prometheus binaries, arch:$ARCH, version:${prometheus_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$PROMETHEUS_URL" || { logger error "downloading prometheus failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$PROMETHEUS_URL" || { logger error "downloading prometheus failed"; exit 1; }
    fi
    mv -f "./prometheus-${prometheus_version}.linux-amd64.tar.gz" "$BASE/"
  fi
}

function download_node_exporter() {
  NODE_EXPORTER_URL="https://github.com/prometheus/node_exporter/releases/download/v${node_exporter_version}/node_exporter-${node_exporter_version}.linux-amd64.tar.gz"


  if [[ -f "$BASE/node_exporter-${node_exporter_version}.linux-amd64.tar.gz" ]];then
    logger warn "node_exporter binaries already existed"
  else
    logger info "downloading node_exporter binaries, arch:$ARCH, version:${node_exporter_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$NODE_EXPORTER_URL" || { logger error "downloading node_exporter failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$NODE_EXPORTER_URL" || { logger error "downloading node_exporter failed"; exit 1; }
    fi
    mv -f "./node_exporter-${node_exporter_version}.linux-amd64.tar.gz" "$BASE/"
  fi
}

function download_alertmanager() {
  ALERTMANAGER_URL="https://github.com/prometheus/alertmanager/releases/download/v${alertmanager_version}/alertmanager-${alertmanager_version}.linux-amd64.tar.gz"

  if [[ -f "$BASE/alertmanager-${alertmanager_version}.linux-amd64.tar.gz" ]];then
    logger warn "alertmanager binaries already existed"
  else
    logger info "downloading alertmanager binaries, arch:$ARCH, version:${alertmanager_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$ALERTMANAGER_URL" || { logger error "downloading alertmanager failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$ALERTMANAGER_URL" || { logger error "downloading alertmanager failed"; exit 1; }
    fi
    mv -f "./alertmanager-${alertmanager_version}.linux-amd64.tar.gz" "$BASE/"
  fi
}

function download_elasticsearch() {
  ELASTICSEARCH_URL="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${elasticsearch_version}-linux-x86_64.tar.gz"

  if [[ -f "$BASE/elasticsearch-${elasticsearch_version}-linux-x86_64.tar.gz" ]];then
    logger warn "elasticsearch binaries already existed"
  else
    logger info "downloading elasticsearch binaries, arch:$ARCH, version:${elasticsearch_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$ELASTICSEARCH_URL" || { logger error "downloading elasticsearch failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$ELASTICSEARCH_URL" || { logger error "downloading elasticsearch failed"; exit 1; }
    fi
    mv -f "./elasticsearch-${elasticsearch_version}-linux-x86_64.tar.gz" "$BASE/"
  fi
}

function download_kibana() {
  KIBANA_URL="https://artifacts.elastic.co/downloads/kibana/kibana-${kibana_version}-linux-x86_64.tar.gz"

  if [[ -f "$BASE/kibana-${kibana_version}-linux-x86_64.tar.gz" ]];then
    logger warn "kibana binaries already existed"
  else
    logger info "downloading kibana binaries, arch:$ARCH, version:${kibana_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$KIBANA_URL" || { logger error "downloading kibana failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$KIBANA_URL" || { logger error "downloading kibana failed"; exit 1; }
    fi
    mv -f "./kibana-${kibana_version}-linux-x86_64.tar.gz" "$BASE/"
  fi
}

function download_filebeat() {
  FILEBEAT_URL="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${filebeat_version}-linux-x86_64.tar.gz"

  if [[ -f "$BASE/filebeat-${filebeat_version}-linux-x86_64.tar.gz" ]];then
    logger warn "filebeat binaries already existed"
  else
    logger info "downloading filebeat binaries, arch:$ARCH, version:${filebeat_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$FILEBEAT_URL" || { logger error "downloading filebeat failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$FILEBEAT_URL" || { logger error "downloading filebeat failed"; exit 1; }
    fi
    mv -f "./filebeat-${filebeat_version}-linux-x86_64.tar.gz" "$BASE/"
  fi
}

function download_logstash() {
  LOGSTASH_URL="https://artifacts.elastic.co/downloads/logstash/logstash-${logstash_version}-linux-x86_64.tar.gz"

  if [[ -f "$BASE/logstash-${logstash_version}-linux-x86_64.tar.gz" ]];then
    logger warn "logstash binaries already existed"
  else
    logger info "downloading logstash binaries, arch:$ARCH, version:${logstash_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$LOGSTASH_URL" || { logger error "downloading logstash failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$LOGSTASH_URL" || { logger error "downloading logstash failed"; exit 1; }
    fi
    mv -f "./logstash-${logstash_version}-linux-x86_64.tar.gz" "$BASE/"
  fi
}

function download_harbor() {
  HARBOR_URL="https://github.com/goharbor/harbor/releases/download/v${harbor_version}/harbor-offline-installer-v${harbor_version}.tgz"


  if [[ -f "$BASE/harbor-offline-installer-v${harbor_version}.tgz" ]];then
    logger warn "harbor binaries already existed"
  else
    logger info "downloading harbor binaries, arch:$ARCH, version:${harbor_version}"
    if [[ -e /usr/bin/wget ]];then
      wget -c --no-check-certificate "$HARBOR_URL" || { logger error "downloading harbor failed"; exit 1; }
    else
      curl -k -C- -O --retry 3 "$HARBOR_URL" || { logger error "downloading harbor failed"; exit 1; }
    fi
    mv -f "./harbor-offline-installer-v${harbor_version}.tgz" "$BASE/"
  fi
}

function download_gitlab-runner-image() {
  echo ${harbor_version}
  if [[ -f "$BASE/gitlab-runner-image.tar" ]];then
    logger warn "gitlab-runner binaries already existed"
  else
    logger info "downloading gitlab-runner image, arch:$ARCH, image:${gitlab_runner_image}"
    if [[ -e /usr/bin/docker ]];then
      docker pull ${gitlab_runner_image} || { logger error "downloading gitlab-runner image failed"; exit 1; }
      docker save ${gitlab_runner_image} > gitlab-runner-image.tar || { logger error "zip gitlab-runner image failed"; exit 1; }
    else
      logger error "need docker" && exit 1
    fi
    mv -f "./gitlab-runner-image.tar" "$BASE/"
  fi
}

function download_all() {
  download_mysql && \
  download_docker && \
  download_zookeeper && \
  download_nginx && \
  download_keepalived && \
  download_jdk && \
  download_maven && \
  download_kafka && \
  download_prometheus && \
  download_node_exporter && \
  download_alertmanager && \
  download_elasticsearch && \
  download_kibana && \
  download_filebeat && \
  download_logstash && \
  download_harbor && \
  download_gitlab-runner-image
}

### Main Lines ##################################################
function main() {
  BASE=$software_dir
  if [ ! -d "$BASE" ];then
    mkdir -p "$BASE"
  fi

  # check if use bash shell
  readlink /proc/$$/exe|grep -q "bash" || { logger error "you should use bash shell, not sh"; exit 1; }
  # check if use with root
  [[ "$EUID" -ne 0 ]] && { logger error "you should run this script as root"; exit 1; }

  # get architecture
  ARCH=$(uname -m)

  [[ "$#" -eq 0 ]] && { usage >&2; exit 1; }
  
  ACTION=""
  while getopts "CDP:RSX:d:e:k:m:z:" OPTION; do
      case "$OPTION" in
        D)
          ACTION="download_all"
          ;;
        P)
          [[ $OPTARG =~ (mysql|docker|zookeeper|nginx|keepalived|jdk|maven|kafka|prometheus|node_exporter|alertmanager|elasticsearch|kibana|filebeat|logstash|harbor|gitlab-runner-image) ]] || \
          { usage-down-packages; exit 1; }
          ACTION="download_$OPTARG"
          ;;
        ?)
          usage
          exit 1
          ;;
      esac
  done
  
  [[ "$ACTION" == "" ]] && { logger error "illegal option"; usage; exit 1; }
  
  # excute cmd "$ACTION" 
  logger info "Action begin: $ACTION"
  ${ACTION} || { logger error "Action failed: $ACTION"; return 1; }
  logger info "Action successed: $ACTION"
}

main "$@"
